﻿namespace DimensionsHelper.ExcelAddIn.Core.Sql;

/// <summary>
/// SQL脚本生成器的设置类
/// </summary>
public class ScriptSettings : ExcelAddInSettingsBase
{

    /// <summary>
    /// SQL设置的ID
    /// </summary>
    public override string Name { get; set; } = string.Empty;

    /// <summary>
    /// 表头标识符的设置
    /// </summary>
    public HeaderIdentifiers? Identifiers { get; set; }

    /// <summary>
    /// ID字段名
    /// </summary>
    public string? IdFieldName { get; set; }

    /// <summary>
    /// ID字段值是否是字符串类型
    /// </summary>
    public bool IsTextId { get; set; }

    /// <summary>
    /// SQL脚本列的表头内容
    /// </summary>
    public string ScriptColumnHeader { get; set; } = "SQL_SCRIPT";

    /// <summary>
    /// 额外条件的逻辑类型，默认是'且'逻辑
    /// </summary>
    public LogicKinds ExtraLogic { get; set; } = LogicKinds.And;

    /// <summary>
    /// 分类值的更新类型，默认是追加更新
    /// </summary>
    public CategoricalUpdateKinds CategoricalUpdateKind { get; set; } = CategoricalUpdateKinds.Append;

    /// <summary>
    /// 额外列表头的映射表
    /// </summary>
    public Dictionary<string, string>? ExtraMap { get; set; }

    /// <summary>
    /// SQL脚本生成器的字段映射表
    /// </summary>
    public Dictionary<string, FieldSettings>? FieldMap { get; set; }

    /// <summary>
    /// 是否构建数据表
    /// </summary>
    public bool BuildDataTable { get; set; }

    /// <summary>
    /// 统计数据表配置，如果不输出统计数据表，则设置为null
    /// </summary>
    public DataTableSettings DataTable { get; set; } = new();

    
    public override void Recive(object transmission)
    {
        ScriptSettingsTransmission? forTransmission = 
            ReadTransmission(transmission, JsonContext.Default.ScriptSettingsTransmission);
        
        if (forTransmission == null)
        {
            return;
        }

        forTransmission.ToSettings(this);
    }

    public override IExcelAddInTransmission Transmit(string? name)
    {
        var transmission = new ScriptSettingsTransmission
        {
            Identifiers = Identifiers?.Transmit() ?? new HeaderIdentifiersTransmission(),
            IdFieldName = IdFieldName ?? string.Empty,
            IsTextId = IsTextId,
            ScriptColumnHeader = ScriptColumnHeader,
            ExtraLogic = ExtraLogic == LogicKinds.Or ? "OR" : "AND",
            CategoricalUpdateKind = CategoricalUpdateKind == CategoricalUpdateKinds.Reset
                ? "RESET" : "APPEND",
            BuildDataTable = BuildDataTable,
            DataTable = DataTable.Transmit()
        };

        return transmission;
    }

    public override bool Join(object transmission)
    {
        ScriptSettingsTransmission? forTransmission = 
            ReadTransmission(transmission, JsonContext.Default.ScriptSettingsTransmission);

        if (forTransmission == null)
        {
            return false;
        }

        if (Identifiers == null)
        {
            Identifiers = forTransmission.Identifiers.ToSettings();
        }
        else
        {
            Identifiers.Join(forTransmission.Identifiers);
        }

        if (!string.IsNullOrEmpty(forTransmission.IdFieldName))
        {
            IdFieldName = forTransmission.IdFieldName;
        }

        IsTextId = forTransmission.IsTextId;

        if (Enum.TryParse<LogicKinds>(forTransmission.ExtraLogic, true, out var logicKind))
        {
            ExtraLogic = logicKind;
        }

        if (Enum.TryParse<CategoricalUpdateKinds>(forTransmission.CategoricalUpdateKind, true, out var updateKind))
        {
            CategoricalUpdateKind = updateKind;
        }

        if (!string.IsNullOrEmpty(forTransmission.ScriptColumnHeader))
        {
            ScriptColumnHeader = forTransmission.ScriptColumnHeader;
        }

        BuildDataTable = forTransmission.BuildDataTable;
        DataTable.Join(forTransmission.DataTable);

        return true;
    }
}